<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

abstract class DIVB_Controller extends CI_Controller {
	protected $perpage = 5;

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
	}
	
	/*
	 * deal with output
	 * */
	public function _output($output)
	{
		echo $output;
	}
	
	/*
	 * remap functions
	 * */
	public function _remap($method, $params = array())
	{
		//$method = 'process_'.$method;
		if (method_exists($this, $method))
		{
			//echo "remap:".get_class($this).",$method";
			$this->_before();
			$rtn = call_user_func_array(array($this, $method), $params);
			$this->_after();
			return $rtn;
		}
		show_404();
	}
	
	/*
	 * base protected functions
	 * */
	protected function _before(){
		$menu_array['menu_list'] = $this->_menuStr();
		
		$this->load->view('common/header');
		$this->load->view('common/navbar');
		$this->load->view('common/menu',$menu_array);
	}
	
	protected function _after(){
		$this->load->view('common/footer');
	}
	
	/*
	* abstract protected funcitons
	*/
	abstract protected function _map();
	
	/**
	 * _menuStr()
	 * @return:返回页面左侧菜单项列表
	 **/
	private function _menuStr(){
		$rtnArr=array();

		$tmp = array('title'=>'section 1',
			     	'list'=>array(
								array('url'=>'#','name'=>'menu item 1'),
								array('url'=>'#','name'=>'menu item 2')
								)
				);
		array_push($rtnArr,$tmp);

		$tmp = array('title'=>'seciont 2',
			     	'list'=>array(
								array('url'=>'#','name'=>'menu item 3'),
								array('url'=>'#','name'=>'menu item 4')
								)
				);
		array_push($rtnArr,$tmp);

		return $rtnArr;
	}
	
	
	/*
	 * public functions
	 * */
	public function search(){
		$map = $this->_map();
		$map = $map['search'];
		
	}
	
	public function table(){
		$map = $this->_map();
		$map = $map['table'];
		$view_data = $this->table_data($map);
		if($view_data){
			$this->load->view('crud/divb_table',$view_data);
		}
	}
	
	public function add(){
		$map = $this->_map();
		$map = $map['add'];
	}
	
	public function edit(){
		$map = $this->_map();
		$map = $map['edit'];
		echo 'edit';
	}
	
	/*
	 * protected functions
	 * */
	

	/**
	* 
	* @return:
	*
	* $sql_select:查询语句(不含limit)
	* $sql_count:查询总数sql,用于分页
	* $cols: array('数据表字段'=>'表头名')
	* $title: 页面标题
	* $filter: 列数据转换 (如type(1,2,3)转换为('是','否','未知')) example: $filter=array('ticket_status'=>array('1'=>'未领取','2'=>'未使用'));
	* $dos: 操作列(编辑,添加等) array(array('name'=>'编辑','key'=>'id','uri'=>'index.php/test/edit'))
	* 
	**/
	protected function table_data($map){
		$sql_select=$map['sql'];
		$sql_count=$map['sql_count'];
		$cols=$map['cols'];
		$title=$map['title'];
		if(array_key_exists('filter', $map)){
			$filter=$map['filter'];
		}else {
			$filter=array();
		}
		if(array_key_exists('dos', $map)){
			$dos=$map['dos'];
		}else {
			$dos=array();
		}
		
		if(is_array($cols) &&
		   strlen($sql_select)>0 &&
		   strlen($sql_count)>0){
			//计算当前页码,并组织除table_page外的uri
			$page=1;
			$qstr=$_SERVER['PATH_INFO'];	//like 'test/table/2'
			
			$qstArray = @split('/',$qstr);
			$page=1;
			if(count($qstArray)>=4){
				$page=$qstArray[3];
			}
			$uriStr = $qstArray[1].'/'.$qstArray[2];
			
			//保证字段和表头顺序对应
			$thArr = array();
			$tdArr = array();
			foreach($cols as $key=>$value){
				array_push($thArr,$value);
				array_push($tdArr,$key);
			}
			//查询列表
			$list = $this->common_model->query_list($sql_select,$page,$this->perpage);
			//filter
			if(count($filter)>0){
				for($i=0;$i<count($list);$i++){
					foreach($filter as $fk=>$fv){
						$list[$i][$fk]=$fv[$list[$i][$fk]];
					}
				}
			}
			//查询总数
			$countRow = $this->common_model->query($sql_count);
			foreach($countRow[0] as $key=>$value){
				$count=$value;
			}
			
			//建立table_page
			$page_num = @ceil($count / $this->perpage); //总共多少页
			$page     = ($page > $page_num) ? $page_num : ($page = ($page < 1) ? 1 : $page);
			$pageArray = $this->page_data($page, $page_num,$uriStr);
			
			$tmp_data = array('table_page_title'=>$title,
								'table_list'=>$list,
								'table_count'=>$count,
								'table_th'=>$thArr,
								'table_td'=>$tdArr,
								'table_page'=>$page,
								'table_page_num'=>$page_num,
								'table_page_list'=>$pageArray,
								'table_dos'=>$dos
							);
			return $tmp_data;
		}
		return false;
	}
	
	protected function page_data($page,$page_num,$uriStr){
			$pageArray = array();
			if($page > 1) 
			{
				$prePage = $page-1;
				$first = array('url'=>"index.php/$uriStr/",'name'=>'首页');
				$previous = array('url'=>"index.php/$uriStr/".$prePage,'name'=>'上一页');
				array_push($pageArray,$first);
				array_push($pageArray,$previous);
			}
			if($page < $page_num) 
			{
				$nextPage = $page+1;
				$next = array('url'=>"index.php/$uriStr/".$nextPage,'name'=>'下一页');
				$last = array('url'=>"index.php/$uriStr/".$page_num,'name'=>'尾页');
				array_push($pageArray,$next);
				array_push($pageArray,$last);
			}
			return $pageArray;
	}
}

/* End of file */